﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using global::FluentValidation;

using global::ERPStore.Extensions;
using global::ERPStore.Validators;

using CloudSoft.Extensions;

namespace ERPStore.ModelValidators
{
	public class RegistrationUserValidator : AbstractValidator<global::ERPStore.Models.RegistrationUser>
	{
		public RegistrationUserValidator()
		{
			RuleFor(p => p.PresentationId)
				.InclusiveBetween(1, 3)
				.WithMessage("Vous devez indiquer votre sexe");

			RuleFor(p => p.LastName)
				.NotNullOrTrimmedEmpty()
				.WithMessage("Vous devez indiquer votre nom");

			RuleFor(p => p.FirstName)
				.NotNullOrTrimmedEmpty()
				.WithMessage("Vous devez indiquer votre prénom");

			RuleFor(p => p.Email)
				.NotNullOrTrimmedEmpty()
				.WithMessage("Vous devez indiquer votre email");

			RuleFor(p => p.Email)
				.EmailAddress()
				.When(p => !p.Email.IsNullOrTrimmedEmpty())
				.WithMessage("Email non valide");

			RuleFor(p => p.MobileNumber)
				.NotNullOrTrimmedEmpty()
				.When(i => i.PhoneNumber.IsNullOrTrimmedEmpty())
				.When(i => i.ValidationStepName == "RegistrationUser"
						|| i.ValidationStepName == "AnonymousShipping")
				.WithMessage("vous devez indiquer un numéro de mobile");

			RuleFor(p => p.MobileNumber)
				.PhoneNumber(ERPStore.GlobalConfiguration.Configuration.Settings.Country)
				.WithMessage("numéro de mobile invalide");

			RuleFor(p => p.PhoneNumber)
				.NotNullOrTrimmedEmpty()
				.When(i => i.MobileNumber.IsNullOrTrimmedEmpty())
				.When(i => i.ValidationStepName == "RegistrationUser"
						|| i.ValidationStepName == "AnonymousShipping")
				.WithMessage("vous devez indiquer un numéro de téléphone");

			RuleFor(p => p.PhoneNumber)
				.PhoneNumber(ERPStore.GlobalConfiguration.Configuration.Settings.Country)
				.WithMessage("numéro de téléphone invalide");

			// Etape inscription de la société

			RuleFor(p => p.CorporatePhoneNumber)
				.PhoneNumber(ERPStore.GlobalConfiguration.Configuration.Settings.Country)
				.When(i => !i.CorporateName.IsNullOrTrimmedEmpty())
				.When(i => i.ValidationStepName == "Corporate")
				.When(i => !i.CorporatePhoneNumber.IsNullOrTrimmedEmpty())
				.WithMessage("numéro de téléphone d'entreprise invalide");

			RuleFor(p => p.CorporatePhoneNumber)
				.NotNullOrTrimmedEmpty()
				.When(i => !i.CorporateName.IsNullOrTrimmedEmpty())
				.When(i => i.ValidationStepName == "Corporate")
				.WithMessage("le numéro de téléphone d'entreprise est requis");

			RuleFor(p => p.NAFCode)
				.Length(4, 10)
				.When(i => !i.CorporateName.IsNullOrTrimmedEmpty())
				.When(i => i.ValidationStepName == "Corporate" 
					|| i.ValidationStepName == "AnonymousShipping")
				.WithMessage("Format du code NAF est incorrect");

			RuleFor(p => p.SiretNumber)
				.Length(8, 20)
				.When(i => !i.CorporateName.IsNullOrTrimmedEmpty())
				.When(i => i.ValidationStepName == "Corporate" 
					|| i.ValidationStepName == "AnonymousShipping")
				.WithMessage("Format du code Siret incorrect");

			RuleFor(p => p.VATNumber)
				.Length(8, 30)
				.When(i => i.CorporateName.IsNullOrTrimmedEmpty())
				.When(i => i.ValidationStepName == "Corporate" 
					|| i.ValidationStepName == "AnonymousShipping")
				.WithMessage("Format du Numéro de TVA incorrect");

			// Etape inscription de l'adresse de facturation

			RuleFor(m => m.BillingAddressRecipientName)
				.NotNullOrTrimmedEmpty()
				.When(i => i.ValidationStepName == "BillingAddress")
				.WithMessage("Vous devez indiquer un nom");

			RuleFor(m => m.BillingAddressStreet)
				.NotNullOrTrimmedEmpty()
				.When(i => i.ValidationStepName == "BillingAddress")
				.WithMessage("Vous devez indiquer une rue ou un lieu dit");

			RuleFor(m => m.BillingAddressZipCode)
				.NotNullOrTrimmedEmpty()
				.When(i => i.ValidationStepName == "BillingAddress")
				.WithMessage("Vous devez indiquer un code postal");

			RuleFor(m => m.BillingAddressZipCode)
				.ZipCode(m => ERPStore.Models.Country.GetByKey(m.BillingAddressCountryId))
				.When(i => i.ValidationStepName == "BillingAddress")
				.WithMessage("Code postal invalide");

			RuleFor(m => m.BillingAddressCity)
				.NotNullOrTrimmedEmpty()
				.When(i => i.ValidationStepName == "BillingAddress")
				.WithMessage("Vous devez indiquer une ville");
		}
	}
}
